package com.test.nnooolwdeaer.activity_fragment_wooden.loan_wooden;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.NiriBankAccountList_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.view_wooden.view_inflater_wooden.BankAccountViewInflater_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.TitleView_wooden;
import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseLoanActivity_wooden;

import org.json.JSONObject;

import java.util.ArrayList;

//import co.paystack.android.Paystack;
//import co.paystack.android.PaystackSdk;
//import co.paystack.android.Transaction;
//import co.paystack.android.model.Card;
//import co.paystack.android.model.Charge;

public class RepayPaystackActivity_Niri_wooden extends BaseLoanActivity_wooden {

    private LinearLayout groupLayout_wooden;
    private Button addBankCardBtn_wooden, repayBtn_wooden;

    private final ArrayList<BankAccountViewInflater_wooden> bankAccountViewInflaters_wooden = new ArrayList<>();

    private int checkIndex_wooden = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repay_paystack_niri_wooden);
        init_wooden();
    }

    private void init_wooden(){
        TitleView_wooden titleView = findViewById(R.id.activity_repay_paystack_list_view_title);
        setBackBtn_wooden(titleView.getBackIb_wooden());

        groupLayout_wooden = findViewById(R.id.activity_repay_paystack_list_layout_group);
        addBankCardBtn_wooden = findViewById(R.id.activity_repay_paystack_list_btn_add_bank_card);
        repayBtn_wooden = findViewById(R.id.activity_repay_paystack_list_btn_repay);

        addBankCardBtn_wooden.setOnClickListener(v -> startActivity_wooden(AddPaystackBankAccountActivity_Niri_wooden.class));

        repayBtn_wooden.setOnClickListener(v -> {
            if (NiriBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().size()==0){
                showToast_wooden("Please add a bank account first");
                return;
            }
            if (checkIndex_wooden <0|| checkIndex_wooden >= NiriBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().size()){
                showToast_wooden("Please select a bank account first");
                return;
            }
            NiriBankAccountList_wooden.NiriBankAccount bankAccount = NiriBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().get(checkIndex_wooden);
            HttpManager_wooden.getInstance_wooden().orderRepaySDK_wooden(bankAccount.getCardno(), bankAccount.getExpiryYear(), bankAccount.getExpiryMonth(), bankAccount.getCvv());
        });

        updateBankAccountList_wooden();
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpManager_wooden.getInstance_wooden().bankList_niri_wooden();
    }

    public void onFetchBankListResponse_wooden(){
        updateBankAccountList_wooden();
    }

    private void updateBankAccountList_wooden(){
        bankAccountViewInflaters_wooden.clear();
        ArrayList<NiriBankAccountList_wooden.NiriBankAccount> bankAccounts = NiriBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden();
        for (int i = 0;i<bankAccounts.size();i++){
            addBankAccount_wooden(bankAccounts.get(i),i);
        }

        for (int i = 0; i< bankAccountViewInflaters_wooden.size(); i++){
            BankAccountViewInflater_wooden bankAccountViewInflater = bankAccountViewInflaters_wooden.get(i);
            final int finalI = i;
            bankAccountViewInflater.setOnClickListener_wooden(v -> {
                for (int j = 0; j< bankAccountViewInflaters_wooden.size(); j++){
                    bankAccountViewInflaters_wooden.get(j).setCheck_wooden(finalI==j);
                    checkIndex_wooden = finalI;
                }
            });
        }
    }

    @SuppressLint("InflateParams")
    private void addBankAccount_wooden(NiriBankAccountList_wooden.NiriBankAccount bankAccount, int index){
        if (bankAccount==null) return;
        View bankAccountView = LayoutInflater.from(this).inflate(R.layout.item_bank_account_niri_wooden,null);
        BankAccountViewInflater_wooden bankAccountViewInflater = new BankAccountViewInflater_wooden(bankAccountView,index);
        bankAccountViewInflater.setBankName_wooden(bankAccount.getBankName());
        bankAccountViewInflater.setBankAccount_wooden(bankAccount.getCardno());
        bankAccountViewInflaters_wooden.add(bankAccountViewInflater);

    }

    public void onOrderRepaySDKResponse_wooden(JSONObject jsonObject){
//        if (NiriBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().size()==0|| checkIndex_wooden <0|| checkIndex_wooden >= NiriBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().size()){
//            return;
//        }
//        NiriBankAccountList_wooden.NiriBankAccount bankAccount = NiriBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().get(checkIndex_wooden);
//        /*
//           accessCode: String,
//           pubKey: String
//         */
//        PaystackSdk.initialize(this);
//        String accessCode = jsonObject.optString("accessCode");
//        String pubKey = jsonObject.optString("pubKey");
//        if (TextUtils.isEmpty(accessCode) || TextUtils.isEmpty(pubKey)) {
//            showToast_wooden("Operation failed, please try again later.");
//            return;
//        }
//
//        String cardNumber = bankAccount.getCardno();
//        String expiryYear = bankAccount.getExpiryYear();
//        String expiryMonth = bankAccount.getExpiryMonth();
//        String cvv = bankAccount.getCvv();
//        Card card = new Card(cardNumber, Integer.parseInt(expiryMonth), Integer.parseInt(expiryYear), cvv);
////        if (!card.isValid()) {
////            showToast("Please enter the correct card number");
////            return;
////        }
//
//        Charge charge = new Charge();
//        charge.setCard(card);
//        charge.setAccessCode(accessCode);
//        PaystackSdk.setPublicKey(pubKey);
//        PaystackSdk.chargeCard(this, charge, new Paystack.TransactionCallback() {
//            @Override
//            public void onSuccess(Transaction transaction) {
//                showToast_wooden("Successful repayment");
//                ThreadUtil_wooden.getInstance_wooden().postDelay_wooden(() -> HttpManager_wooden.getInstance_wooden().requestUserProcess_wooden(), 1000);
//            }
//            @Override
//            public void beforeValidate(Transaction transaction) {}
//            @Override
//            public void onError(Throwable error, Transaction transaction) {
//                showToast_wooden(transaction.getReference());
//            }
//        });
    }
}